//
//  ShareButton.m
//  DaDaBao
//
//  Created by admin on 15/9/1.
//  Copyright (c) 2015年 User. All rights reserved.
//

#import "ShareButton.h"
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
@interface ShareButton()
{
    UILabel *titleLabel;
    UIImageView *titleImageView;
    UIView *btnView;
    UIImageView *badgeImageView;//红色的点
}
@end

@implementation ShareButton

- (instancetype)initWithFrame:(CGRect)frame image:(UIImage *)image title:(NSString *)title
{
    if (self = [super initWithFrame:frame]) {
        
        btnView = [self btnViewWithImage:image title:title];
        
        [self addSubview:btnView];
        
        badgeImageView = [[UIImageView alloc] init];
        [self addSubview:badgeImageView];
    }
    return self;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        btnView = [[UIView alloc] init];
        [self addSubview:btnView];
        
        titleImageView = [[UIImageView alloc] init];
        [btnView addSubview:titleImageView];
        
        titleLabel = [[UILabel alloc] init];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        [btnView addSubview:titleLabel];
        
        badgeImageView = [[UIImageView alloc] init];
        [self addSubview:badgeImageView];
    }
    return self;
}
- (UIView *)btnViewWithImage:(UIImage *) image title:(NSString *)title
{
    UIView *view = [[UIView alloc] init];
    view.userInteractionEnabled = NO;
    CGFloat viewWidth = self.frame.size.width;
    
    
    CGFloat viewHeight = image.size.height+10.0f+20.0f;
    
    
    view.frame = CGRectMake(0.0f, (self.frame.size.height-viewHeight)/2, viewWidth, viewHeight);
    
    titleImageView = [[UIImageView alloc] initWithFrame:CGRectMake((view.frame.size.width-image.size.width)/2, 0.0f, image.size.width, image.size.height)];
    titleImageView.userInteractionEnabled = NO;
    titleImageView.image = image;
    [view addSubview:titleImageView];
    
    titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, titleImageView.frame.origin.y+titleImageView.frame.size.height+ 10.0f, view.frame.size.width, 20.0f)];
    titleLabel.text = title;
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.userInteractionEnabled  = NO;
    [view addSubview:titleLabel];
    
    
    
    return view;
}
//设置按钮的标题字体
- (void)setTitleLabelFont:(UIFont *)font
{
    [titleLabel setFont:font];
    
    NSDictionary *attributesDic = @{NSFontAttributeName:font};
    CGSize size = [@"测试" boundingRectWithSize:CGSizeMake(titleLabel.frame.size.width, CGFLOAT_MAX)
                                      options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesFontLeading|NSStringDrawingUsesDeviceMetrics
                                   attributes:attributesDic
                                      context:nil].size;
    
    
    titleLabel.frame = CGRectMake(0.0f, titleImageView.frame.origin.y+titleImageView.frame.size.height+ 10.0f, titleLabel.frame.size.width, size.height);
    
    CGFloat viewHeight = titleImageView.frame.size.height+10.0f+size.height;
    
    btnView.frame = CGRectMake(0.0f, (self.frame.size.height-viewHeight)/2, btnView.frame.size.width, viewHeight);
}
//设置按钮的图片
- (void)setTitleImage:(UIImage *)image
{
    titleImageView.frame = CGRectMake((self.frame.size.width-image.size.width)/2, 0.0f, image.size.width, image.size.height);
    titleImageView.image = image;
    
    CGFloat viewHeight = titleImageView.frame.size.height+10.0f+titleLabel.frame.size.height+10.0f;
    
    btnView.frame = CGRectMake(0.0f, (self.frame.size.height-viewHeight)/2, btnView.frame.size.width, viewHeight);
}
//设置按钮的标题
- (void)setTitleLabelString:(NSString *)title
{
    titleLabel.frame = CGRectMake(0.0f, titleImageView.frame.origin.y+titleImageView.frame.size.height+ 10.0f, self.frame.size.width, 20.0f);
    titleLabel.text = title;
    [self setTitleLabelFont:titleLabel.font];
    
    CGFloat viewHeight = titleImageView.frame.size.height+10.0f+titleLabel.frame.size.height;
    
    btnView.frame = CGRectMake(0.0f, (self.frame.size.height-viewHeight)/2, btnView.frame.size.width, viewHeight);
}
//设置字体颜色
- (void)setTitleLabelTextColor:(UIColor *)color
{
    titleLabel.textColor = color;
}

- (void)setIsShowBadge:(BOOL)isShowBadge
{
    if (isShowBadge == NO) {
        
        badgeImageView.hidden = YES;
    }else
    {
        badgeImageView.image = [UIImage imageNamed:@"red_count.png"];
        badgeImageView.frame = CGRectMake(self.frame.size.width-10.0f-7.0f, 10.0f, 7.0f, 7.0f);
        //        badgeImageView.backgroundColor = UIColorFromRGB(0xe94650);
        badgeImageView.layer.cornerRadius = 7.0f/2;
        badgeImageView.layer.masksToBounds = YES;
        badgeImageView.hidden = NO;
    }
}

@end
